class Solution {
    int ret, _m, _n, _cnt;
    bool vis[101][101];
    int dx[4] = {0, 0, -1, 1};
    int dy[4] = {1, -1, 0, 0};
 
public:
    int wardrobeFinishing(int m, int n, int cnt) {
        _m = m, _n = n, _cnt = cnt;
        dfs(0, 0);
        return ret;
    }
 
    void dfs(int sr, int sc)
    {
        ++ret;
        vis[sr][sc] = true;
        for(int i = 0; i < 4; ++i)
        {
            int x = sr + dx[i], y = sc + dy[i];
            if(x >= 0 && x < _m && y >= 0 && y < _n && !vis[x][y] && chick(x, y))
                dfs(x, y);
        }
    }
 
    bool chick(int x, int y)
    {
        int sum = 0;
        while(x)
        {
            sum += (x % 10);
            x /= 10;
        }
        while(y)
        {
            sum += (y % 10);
            y /= 10;
        }
        return sum <= _cnt;
    }
};